<template>
  <header class="header">
    <symbol id="icon-cart" viewBox="0 0 38 32">
      <title>购物车</title>
      <path class="path1" d="M37.759 0h-4.133c-0.733 0.004-1.337 0.549-1.434 1.255l-0.546 4.342c-0.081 0.484-0.496 0.849-0.997 0.849-0.005 0-0.009-0-0.014-0h-27.604c-0.003 0-0.007-0-0.011-0-1.674 0-3.031 1.357-3.031 3.031 0 0.34 0.056 0.666 0.159 0.971l2.52 8.062c0.385 1.194 1.486 2.043 2.785 2.043 0.126 0 0.25-0.008 0.372-0.023l22.983 0.002c0.515 0.131 0.626 0.768 0.626 1.283 0.005 0.044 0.009 0.095 0.009 0.146 0 0.501-0.294 0.933-0.718 1.134l-22.439 0.003c-0.354 0-0.642 0.287-0.642 0.642s0.287 0.642 0.642 0.642h22.745l0.131-0.071c0.919-0.392 1.551-1.287 1.551-2.33 0-0.058-0.002-0.116-0.006-0.173 0.021-0.108 0.033-0.24 0.033-0.376 0-1.072-0.732-1.973-1.724-2.23l-23.357-0.004c-0.063 0.008-0.135 0.013-0.209 0.013-0.719 0-1.332-0.455-1.566-1.093l-2.53-8.095c-0.048-0.154-0.076-0.332-0.076-0.515 0-0.973 0.782-1.764 1.752-1.778h27.657c1.159-0.004 2.112-0.883 2.232-2.011l0.547-4.345c0.010-0.083 0.078-0.147 0.161-0.152l4.133-0c0.354 0 0.642-0.287 0.642-0.642s-0.287-0.642-0.642-0.642z"></path>
      <path class="path2" d="M31.323 9.69c-0.022-0.003-0.048-0.004-0.074-0.004-0.328 0-0.598 0.248-0.633 0.567l-0.809 7.268c-0.003 0.022-0.004 0.048-0.004 0.074 0 0.328 0.248 0.598 0.567 0.633l0.074 0c0.001 0 0.003 0 0.004 0 0.327 0 0.596-0.246 0.632-0.563l0.809-7.268c0.003-0.022 0.004-0.048 0.004-0.074 0-0.328-0.248-0.598-0.567-0.633z"></path>
      <path class="path3" d="M27.514 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM27.514 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
      <path class="path4" d="M9.599 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM9.599 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
    </symbol>
    <div class="navbar">
      <div class="navbar-left-container">
        <a href="/">
          <img class="navbar-brand-logo" src="static/logo.png">
        </a>
      </div>
      <div class="navbar-right-container" style="display: flex;">
        <div class="navbar-menu-container">
<!--          <span v-if="nickName">欢迎：</span>-->
<!--          <span v-text="nickName" v-if="nickName"></span>-->


<!--          <a href="javascript:void(0)" @click="modifyModalFlag=true" title="我的账户" v-if="nickName" class="navbar-link"><i class="el-icon-user-solid">我的账户</i></a>-->
<!--          <span class="navbar-link"></span>-->
          <a href="/#/login" class="navbar-link" @click="loginModalFlag=true" v-if="!nickName">你好，请登录</a>
          <a href="/#/OrderList" class="navbar-link" v-if="nickName"> <i class="el-icon-s-order">我的订单</i></a>
          <a href="/#/PointsExchange" class="navbar-link" v-if="nickName"> <i class="el-icon-present">积分兑换</i></a>
          <a href="/#/FeedBackList" class="navbar-link" v-if="nickName"> <i class="el-icon-s-comment">建议反馈</i></a>
<!--          <a href="javascript:void(0)" class="navbar-link" v-if="nickName" @click="logout"> <i class="el-icon-s-unfold">登出</i></a>-->
          <div class="navbar-cart-container">
            <span class="navbar-cart-count" v-if="cartCount>0">{{cartCount}}</span>
            <a class="navbar-link navbar-cart-link" href="/#/cart" v-if="nickName">
              <svg class="navbar-cart-logo">
                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-cart"></use>
              </svg>
            </a>
          </div>

          <el-dropdown v-if="nickName" class="navbar-link">
            <span>欢迎{{nickName}}<i class="el-icon-s-custom" style="margin-right:15px"></i></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <a href="javascript:void(0)" @click="modifyModalFlag=true" title="我的账户" v-if="nickName" class="navbar-link"><i class="el-icon-user-solid">我的账户</i></a>
              </el-dropdown-item>
<!--              <el-dropdown-item>-->
<!--                <a href="javascript:void(0)" @click="modifyModalFlag=true" title="地址管理" v-if="nickName" class="navbar-link"><i class="el-icon-location">地址管理</i></a>-->
<!--              </el-dropdown-item>-->
              <el-dropdown-item>
                <a href="javascript:void(0)" class="navbar-link" v-if="nickName" @click="logout"> <i class="el-icon-s-unfold">登出</i></a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </div>
      </div>
    </div>
    <!-- 登录 -->
    <div class="md-modal modal-msg md-modal-transition" v-bind:class="{'md-show':loginModalFlag}">
      <div class="md-modal-inner">
        <div class="md-top">
          <div class="md-title">登录</div>
          <button class="md-close" @click="loginModalFlag=false">关闭</button>
        </div>
        <div class="md-content">
          <div class="confirm-tips">
            <div class="error-wrap">
              <span class="error error-show" v-show="errorTip">用户名或密码错误</span>
            </div>
            <ul>
              <li class="regi_form_input">
                <i class="icon IconPeople"></i>
                <input type="text" tabindex="1" name="loginname" v-model="userName" placeholder="User Name" data-type="loginname" class="regi_login_input regi_login_input_left">
              </li>
              <li class="regi_form_input noMargin">
                <i class="icon IconPwd"></i>
                <input type="password" tabindex="2" name="password" v-model="userPwd" placeholder="Password" class="regi_login_input regi_login_input_left login-input-no input_text"
                  @keyup.enter="login">
              </li>
            </ul>
          </div>
          <div class="login-wrap">
            <a href="javascript:;" class="btn-login" @click="login">登 录</a>
          </div>
          <div class="login-wrap">
            <a href="javascript:;" class="btn-login" @click="registerModalFlag=true;loginModalFlag=false;">注 册</a>
          </div>
        </div>
      </div>

    </div>
    <!-- 注册 -->
    <div class="md-modal modal-msg md-modal-transition" v-bind:class="{'md-show':registerModalFlag}">
      <div class="md-modal-inner">
        <div class="md-top">
          <div class="md-title">注册</div>
          <button class="md-close" @click="registerModalFlag=false">关闭</button>
        </div>
        <div class="md-content">
          <div class="confirm-tips">
            <div class="error-wrap">
              <span class="error error-show" v-show="errorTip">注册失败</span>
            </div>
            <ul>
              <li class="regi_form_input">
                <i class="icon IconPeople"></i>
                <input type="text" tabindex="1" name="loginname" v-model="userName" placeholder="User Name" data-type="loginname" class="regi_login_input regi_login_input_left">
              </li>
              <li class="regi_form_input noMargin">
                <i class="icon IconPwd"></i>
                <input type="password" tabindex="2" name="password" v-model="userPwd" placeholder="Password" class="regi_login_input regi_login_input_left login-input-no input_text"
                  @keyup.enter="login">
              </li>
            </ul>
          </div>
          <div class="login-wrap">
            <a href="javascript:;" class="btn-login" @click="register">注 册</a>
          </div>
        </div>
      </div>

    </div>
    <!-- 修改信息 -->
    <div class="md-modal modal-msg md-modal-transition" v-bind:class="{'md-show':modifyModalFlag}">
      <div class="md-modal-inner">
        <div class="md-top">
          <div class="md-title">更新个人信息</div>
          <button class="md-close" @click="modifyModalFlag=false">关闭</button>
        </div>
        <div class="md-content">
          <div class="confirm-tips">
            <div class="error-wrap">
              <span class="error error-show" v-show="errorTip">修改失败</span>
            </div>
            <el-form ref="editForm" :rules="rules" label-width="80px" :model="editForm">
              <el-form-item label="用户名" prop="userName">
                <el-input disabled v-model="editForm.userName"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="editForm.email"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="tel">
                <el-input v-model="editForm.tel"></el-input>
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="editForm.sex">
                  <el-radio label="男"></el-radio>
                  <el-radio label="女"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="新密码" prop="newPwd">
                <el-input v-model="editForm.newPwd"></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPwd">
                <el-input v-model="editForm.checkPwd"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="login-wrap">
            <a href="javascript:;" class="btn-login" @click="modify">保存个人信息</a>
          </div>
        </div>
      </div>

    </div>
    <div class="md-overlay" v-if="loginModalFlag||registerModalFlag||modifyModalFlag" @click="loginModalFlag=false;registerModalFlag=false;modifyModalFlag=false"></div>
  </header>
</template>

<script>
import axios from "axios";
  export default {
    data() {
      // <!--二次验证密码-->
      let validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.editForm.newPwd) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      return {
        userName: "",
        userPwd: "",
        newPwd: "******",
        errorTip: false,
        loginModalFlag: false,
        registerModalFlag: false,
        modifyModalFlag: false,
        editForm:{
          userName: "",
          sex: "男",
          tel: "",
          email: "",
          newPwd: "",
          checkPwd:""
        },
        rules: {
          userName: [{required: true, message: " 账号不可为空", trigger: 'blur'}],
          tel: [{required: true, message: " 账号不可为空", trigger: 'blur'}],
          checkPwd:[{required: true, validator:validatePass2, trigger: 'blur' }],
          email: [
            {
              required: true,
              message: "请输入邮箱",
              trigger: "blur"
            },
            {
              validator: function(rule, value, callback) {
                if (
                  /^\w{1,64}@[a-z0-9\-]{1,256}(\.[a-z]{2,6}){1,2}$/i.test(
                    value
                  ) == false
                ) {
                  callback(new Error("邮箱格式错误"));
                } else {
                  callback();
                }
              },
              trigger: "blur"
            }
          ]
        }
      };

    },
    mounted() {
      this.checkLogin();
    },
    computed: {
      nickName() {
        return this.$store.state.nickName;
      },
      cartCount() {
        return this.$store.state.cartCount;
      }
    },
    methods: {
      checkLogin() {
        console.log(JSON.parse(sessionStorage.getItem("user")));
        this.$store.commit("updateUserInfo", JSON.parse(sessionStorage.getItem("user")).username);
        this.editForm.userName = JSON.parse(sessionStorage.getItem("user")).username;
        this.editForm.email = JSON.parse(sessionStorage.getItem("user")).email;
        this.editForm.tel = JSON.parse(sessionStorage.getItem("user")).phone;
        if (JSON.parse(sessionStorage.getItem("user")).sex===0)
          this.editForm.sex = "男"
        else
          this.editForm.sex = "女"
        // this.$axios.get("/buyer/check_login").then(response => {
        //   let res = response.data;
        //   if (res.code == "0") {
        //     this.$store.commit("updateUserInfo", res.data.userName);
        //     this.getCartCount();
        //   }
        // });
      },
      modify() {
        let params = {
          userName: this.editForm.userName,
          tel: this.editForm.tel,
          sex: this.editForm.sex,
          email: this.editForm.email,
          newPwd: this.editForm.newPwd,
          checkPwd: this.editForm.checkPwd
        };
        axios.post("/customer/modify", params,{ headers:{content:"application/json;charset=UTF-8" }})
          .then(response => {
            let res = response.data;
            if (res.code === 0) {
              this.errorTip = false;
              this.modifyModalFlag = false;
              this.$message.success(res.reason);
            } else {
              this.$message.error(res.reason);
              this.errorTip = true;
            }
          });
      },
      register() {
        if (!this.userName || !this.userPwd) {
          this.errorTip = true;
          return;
        }
        this.$axios
          .post("/buyer/register", {
            username: this.userName,
            password: this.userPwd
          })
          .then(response => {
            let res = response.data;
            if (res.code == "0") {
              this.errorTip = false;
              this.registerModalFlag = false;
              alert("注册成功");
            } else {
              this.errorTip = true;
              alert(res.msg);
            }
          });
      },
      login() {
        if (!this.userName || !this.userPwd) {
          this.errorTip = true;
          return;
        }
        this.$axios
          .post("/buyer/login", {
            userName: this.userName,
            userPwd: this.userPwd
          })
          .then(response => {
            let res = response.data;
            if (res.code == "0") {
              this.errorTip = false;
              this.loginModalFlag = false;
              this.tel = res.data.tel;
              this.email = res.data.email;
              this.$store.commit("updateUserInfo", res.data.username);
              this.getCartCount();
            } else {
              this.errorTip = true;
            }
          });
      },

      logout() {
        this.$axios.get("/customer/logout").then(response => {
          let res = response.data;
          if (res === 1 ) {
            this.$store.commit("updateUserInfo", "");
            this.$store.commit("initCartCount", 0);
            sessionStorage.clear();
            this.$router.push({
              path: "/"
            });
          } else {
            this.errorTip = true;
          }
        });
      },

      getCartCount() {
        this.$axios
          .get("/buyer/cart/list")
          .then(result => {
            let res = result.data;
            if (res.code == "0") {
              let num = 0;
              res.data.forEach(item => {
                num += item.quantity;
              });
              this.$store.commit("initCartCount", num);
            } else {
            }
          })
          .catch(function (error) {
            console.log("error init." + error);
          });
      }
    }
  };
</script>
